﻿using System;
using Cumulux.BootStrapper.Contracts;
using Moq;
using NUnit.Framework;

namespace Cumulux.BootStrapper.Tests
{
    [TestFixture]
    public class BootStrapperManagerTests
    {
        private string validConnection;
        private string badConnection;
        private Mock<IPackageRunner> runner;
        private Mock<IPackageUnzipper> unzipper;
        private Mock<ILogger> logger;
        private MockRepository mockRepo;
        private Mock<IPackageDownloader> downloader;

        [SetUp]
        public void Init()
        {
            this.validConnection = @"DefaultEndpointsProtocol=https;AccountName=test;AccountKey=Y820N+5Qjbyl6Ws8jKS9SdkVs/ITqPNx3M5AW/pTx72sJd5O3lrskqfhRbzPA3wdExmx7C7qtRMcDxuP49goMQ==";
            this.badConnection = @"DefaultEndpointsProtocol=https;AccountName=test;AccountKey=SomeKey";
            mockRepo = new MockRepository(MockBehavior.Strict);
            this.runner = mockRepo.Create<IPackageRunner>();
            this.unzipper = mockRepo.Create<IPackageUnzipper>();
            this.logger = mockRepo.Create<ILogger>();
            this.downloader = mockRepo.Create<IPackageDownloader>();
        }

        [Test]
        public void TestSimpleGet()
        {
            var uri = new Uri(@"http://contoso.com/installer.msi");

            downloader.Setup(m => m.DownloadPackageToDisk(uri, false, @"C:\Temp"))
                      .Returns(@"c:\temp");

            var args = new BootStrapperArgs()
            {
                Get = uri.ToString(),
                LocalResource = @"C:\Temp"
            };

            var manager = new BootStrapperManager(logger.Object, downloader.Object, runner.Object, unzipper.Object);
            manager.Start(args);

            downloader.VerifyAll();
        }

        [Test]
        public void TestAzureGetWithSlash()
        {
            downloader.Setup(m => m.DownloadPackageToDisk(
                It.Is<Uri>(u => u.ToString().StartsWith(@"https://test.blob.core.windows.net/container/large.rar")),
                false,
                @"C:\Temp"))
                .Returns(@"c:\temp");
            

            var args = new BootStrapperArgs()
            {
                Get = "/container/large.rar",
                LocalResource = @"C:\Temp",
                StorageConnection = this.validConnection
            };

            var manager = new BootStrapperManager(logger.Object, downloader.Object, runner.Object, unzipper.Object);
            manager.Start(args);

            downloader.VerifyAll();
        }

        [Test]
        public void TestAzureGetWithoutSlash()
        {
            downloader.Setup(m => m.DownloadPackageToDisk(It.Is<Uri>(u => u.ToString().StartsWith(@"https://test.blob.core.windows.net/container/large.rar")),
                                                          It.Is<bool>(b => b == false),
                                                          It.Is<string>(s => string.Equals(s, @"C:\Temp"))))
                                                          .Returns(@"c:\temp")
                                                          .Verifiable();
            var args = new BootStrapperArgs()
            {
                Get = "container/large.rar",
                LocalResource = @"C:\Temp",
                StorageConnection = this.validConnection
            };

            var manager = new BootStrapperManager(logger.Object, downloader.Object, runner.Object, unzipper.Object);
            manager.Start(args);

            downloader.VerifyAll();
        }

        [Test]
        public void TestUnzip()
        {
            downloader.Setup(m => m.DownloadPackageToDisk(It.IsAny<Uri>(), It.IsAny<bool>(), It.IsAny<string>())).Returns(@"C:\Temp\testing.zip").Verifiable();

            unzipper.Setup(m => m.Unzip(It.Is<string>(s => string.Equals(s, @"C:\Temp\testing.zip")), It.Is<string>(s => s == @"C:\Temp"), It.Is<bool>(b => b == false))).Verifiable();

            var args = new BootStrapperArgs()
            {
                Get = "http://contoso.net/downloads/testing.zip",
                LocalResource = @"C:\Temp", // when run on bootstrapper.exe, this is assiged to whatever unziptarget is
                UnzipTarget = @"C:\Temp",
                Unzip = true
            };

            var manager = new BootStrapperManager(logger.Object, downloader.Object, runner.Object, unzipper.Object);
            manager.Start(args);

            unzipper.VerifyAll();
        }
    }
}
